// pages/courseDetail/courseDetail.js
const WxParse = require('../../../wxParse/wxParse.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        titlebarRatio: 1,
        // 热门课程
        hotCourseData: [],
        navigation: {
            title: "课程详情", //页面标题
            isLeftShow: true, //是否显示左箭头
            color: '#fff' //字体颜色
        },
        detail: null,
        order: null,
        isShowVideo: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.data.goodsId = options.id;
        this.onPullDownRefresh();

        wx.emitter.on('course.update', this.onPullDownRefresh);
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function(options) {
        wx.emitter.off('course.update', this.onPullDownRefresh);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.loadHotCourseData();
        this.loadDetail().finally(() => {
            wx.stopPullDownRefresh();
        });
    },

    // 页面被滚动
    onPageScroll: function(e) {
        const shakeScrollTop = e.scrollTop * 0.7;
        let ratio = parseFloat(shakeScrollTop / 100).toFixed(2);
        if (ratio > 1) ratio = 1;
        ratio = 1 - ratio;
        if (this.data.titlebarRatio === ratio) return;
        this.setData({
            titlebarRatio: ratio
        });
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        const image = "";
        return {
            imageUrl: image,
            path: this.route + `?id=${this.data.goodsId}`
        }
    },

    // 加载商品详情
    loadDetail: function() {
        return wx.http.get('api/curriculum/detail', {
            goods_id: this.data.goodsId
        }).then((res) => {
            const detail = res.data.data.detail;
            const order = res.data.data.order;
            const describe = res.data.data.describe;
            this.setData({
                detail: detail,
                order: order && order.goods ? order : null,
                describe: describe,
            }, () => {
                WxParse.wxParse('detailContent', 'html', detail.content, this);
            });
            return detail;
        }, (err) => {
            console.warn("api/curriculum/detail", err)
        });
    },

    // 加载热门课程
    loadHotCourseData: function() {
        return wx.http.get('api/curriculum/lists', {
            page: 1,
            type: 'popular',
            sortType: 'all',
            sortPrice: 0,
            search: '',
            course_id: '',
            category_id: '',
        }).then((res) => {
            res = res.data.data.list;
            this.setData({
                hotCourseData: res.data,
            });
            return res.data;
        }, (err) => {
            console.warn("api/curriculum/lists", err)
        });
    },

    // 收藏
    collectionTap: function() {
        return wx.http.post('api/curriculum/collect', {
            goods_id: this.data.goodsId,
        }).then((res) => {
            const isCollect = !this.data.detail.isCollect;
            this.setData({
                'detail.isCollect': isCollect
            });
            return res.data.data;
        }, (err) => {
            console.warn("api/curriculum/collect", err)
        });
    },

    // 预览图片
    previewTap: function(e) {
        const idx1 = e.currentTarget.dataset.idx,
            idx2 = e.target.dataset.idx;
        if (idx1 === undefined || idx2 === undefined) return;

        const images = this.data.detail.comment_data[idx1].image.map(it => it.file_path);
        wx.previewImage({
            urls: images,
            current: images[idx2]
        });
    }
});